
Trajectories and TrajectoryCollections can be converted back to GeoDataFrames that can then be exported to different GIS file formats.
import pandas as pd
import geopandas as gpd
import movingpandas as mpd
import shapely as shp
import hvplot.pandas
from geopandas import GeoDataFrame, read_file
from shapely.geometry import Point, LineString, Polygon
from datetime import datetime, timedelta
from holoviews import opts
import warnings
warnings.filterwarnings('ignore')
opts.defaults(opts.Overlay(active_tools=['wheel_zoom'], frame_width=500, frame_height=400))
mpd.show_versions()
MovingPandas 0.10.rc1 SYSTEM INFO ----------- python : 3.9.13 | packaged by conda-forge | (main, May 27 2022, 16:50:36) [MSC v.1929 64 bit (AMD64)] executable : H:\miniconda3\envs\mpd-ex\python.exe machine : Windows-10-10.0.19043-SP0 GEOS, GDAL, PROJ INFO --------------------- GEOS : None GEOS lib : None GDAL : 3.5.0 GDAL data dir: None PROJ : 9.0.0 PROJ data dir: H:\miniconda3\pkgs\proj-9.0.0-h1cfcee9_1\Library\share\proj PYTHON DEPENDENCIES ------------------- geopandas : 0.10.2 pandas : 1.4.2 fiona : 1.8.21 numpy : 1.22.4 shapely : 1.8.2 rtree : 1.0.0 pyproj : 3.3.1 matplotlib : 3.5.2 mapclassify: 2.4.3 geopy : 2.2.0 holoviews : 1.14.9 hvplot : 0.8.0 geoviews : 1.9.5 stonesoup : 0.1b9
gdf = read_file('../data/geolife_small.gpkg')
traj_collection = mpd.TrajectoryCollection(gdf, 'trajectory_id', t='t')
traj_collection.to_point_gdf()
| id | sequence | trajectory_id | tracker | geometry | |
|---|---|---|---|---|---|
| t | |||||
| 2008-12-11 04:42:14 | 1 | 1 | 1 | 19 | POINT (116.39131 39.89857) |
| 2008-12-11 04:42:16 | 2 | 2 | 1 | 19 | POINT (116.39132 39.89862) |
| 2008-12-11 04:43:26 | 3 | 3 | 1 | 19 | POINT (116.39093 39.89861) |
| 2008-12-11 04:43:32 | 4 | 4 | 1 | 19 | POINT (116.39083 39.89863) |
| 2008-12-11 04:43:47 | 5 | 5 | 1 | 19 | POINT (116.38941 39.89872) |
| ... | ... | ... | ... | ... | ... |
| 2009-02-25 14:31:04 | 6993 | 867 | 5 | 2 | POINT (116.33719 39.92623) |
| 2009-02-25 14:31:09 | 6994 | 868 | 5 | 2 | POINT (116.33721 39.92624) |
| 2009-02-25 14:31:14 | 6995 | 869 | 5 | 2 | POINT (116.33726 39.92621) |
| 2009-02-25 14:31:19 | 6996 | 870 | 5 | 2 | POINT (116.33729 39.92620) |
| 2009-02-25 14:31:24 | 6997 | 871 | 5 | 2 | POINT (116.33733 39.92619) |
5908 rows × 5 columns
traj_collection.to_line_gdf()
| id | sequence | trajectory_id | tracker | t | prev_t | geometry | |
|---|---|---|---|---|---|---|---|
| 0 | 2 | 2 | 1 | 19 | 2008-12-11 04:42:16 | 2008-12-11 04:42:14 | LINESTRING (116.39131 39.89857, 116.39132 39.8... |
| 1 | 3 | 3 | 1 | 19 | 2008-12-11 04:43:26 | 2008-12-11 04:42:16 | LINESTRING (116.39132 39.89862, 116.39093 39.8... |
| 2 | 4 | 4 | 1 | 19 | 2008-12-11 04:43:32 | 2008-12-11 04:43:26 | LINESTRING (116.39093 39.89861, 116.39083 39.8... |
| 3 | 5 | 5 | 1 | 19 | 2008-12-11 04:43:47 | 2008-12-11 04:43:32 | LINESTRING (116.39083 39.89863, 116.38941 39.8... |
| 4 | 6 | 6 | 1 | 19 | 2008-12-11 04:43:50 | 2008-12-11 04:43:47 | LINESTRING (116.38941 39.89872, 116.39052 39.8... |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 5898 | 6993 | 867 | 5 | 2 | 2009-02-25 14:31:04 | 2009-02-25 14:31:02 | LINESTRING (116.33715 39.92631, 116.33719 39.9... |
| 5899 | 6994 | 868 | 5 | 2 | 2009-02-25 14:31:09 | 2009-02-25 14:31:04 | LINESTRING (116.33719 39.92623, 116.33721 39.9... |
| 5900 | 6995 | 869 | 5 | 2 | 2009-02-25 14:31:14 | 2009-02-25 14:31:09 | LINESTRING (116.33721 39.92624, 116.33726 39.9... |
| 5901 | 6996 | 870 | 5 | 2 | 2009-02-25 14:31:19 | 2009-02-25 14:31:14 | LINESTRING (116.33726 39.92621, 116.33729 39.9... |
| 5902 | 6997 | 871 | 5 | 2 | 2009-02-25 14:31:24 | 2009-02-25 14:31:19 | LINESTRING (116.33729 39.92620, 116.33733 39.9... |
5903 rows × 7 columns
traj_collection.to_traj_gdf(wkt=True)
| traj_id | start_t | end_t | geometry | length | direction | wkt | |
|---|---|---|---|---|---|---|---|
| 0 | 1 | 2008-12-11 04:42:14 | 2008-12-11 05:15:46 | LINESTRING (116.39131 39.89857, 116.39132 39.8... | 6207.020261 | 186.681376 | LINESTRING M (116.391305 39.898573 1228970534.... |
| 1 | 2 | 2009-06-29 07:02:25 | 2009-06-29 11:13:12 | LINESTRING (116.59096 40.07196, 116.59091 40.0... | 38764.575483 | 250.585295 | LINESTRING M (116.590957 40.071961 1246258945.... |
| 2 | 3 | 2009-02-04 04:32:53 | 2009-02-04 11:20:12 | LINESTRING (116.38569 39.89977, 116.38565 39.8... | 12745.157506 | 304.115160 | LINESTRING M (116.385689 39.899773 1233721973.... |
| 3 | 4 | 2009-03-10 10:36:45 | 2009-03-10 12:01:07 | LINESTRING (116.38805 39.90342, 116.38804 39.9... | 14363.780551 | 300.732843 | LINESTRING M (116.388053 39.903418 1236681405.... |
| 4 | 5 | 2009-02-25 09:47:03 | 2009-02-25 14:31:24 | LINESTRING (116.38526 39.90027, 116.38525 39.9... | 39259.779560 | 305.200501 | LINESTRING M (116.385256 39.90027 1235555223.0... |
These GeoDataFrames can be exported to different file formats using GeoPandas, as documented in https://geopandas.org/docs/user_guide/io.html
export_gdf = traj_collection.to_traj_gdf(wkt=True)
export_gdf.to_file("temp.gpkg", layer='trajectories', driver="GPKG")
read_file('temp.gpkg').plot()
<AxesSubplot:>
read_file('temp.gpkg')
| traj_id | start_t | end_t | length | direction | wkt | geometry | |
|---|---|---|---|---|---|---|---|
| 0 | 1 | 2008-12-11T04:42:14 | 2008-12-11T05:15:46 | 6207.020261 | 186.681376 | LINESTRING M (116.391305 39.898573 1228970534.... | LINESTRING (116.39131 39.89857, 116.39132 39.8... |
| 1 | 2 | 2009-06-29T07:02:25 | 2009-06-29T11:13:12 | 38764.575483 | 250.585295 | LINESTRING M (116.590957 40.071961 1246258945.... | LINESTRING (116.59096 40.07196, 116.59091 40.0... |
| 2 | 3 | 2009-02-04T04:32:53 | 2009-02-04T11:20:12 | 12745.157506 | 304.115160 | LINESTRING M (116.385689 39.899773 1233721973.... | LINESTRING (116.38569 39.89977, 116.38565 39.8... |
| 3 | 4 | 2009-03-10T10:36:45 | 2009-03-10T12:01:07 | 14363.780551 | 300.732843 | LINESTRING M (116.388053 39.903418 1236681405.... | LINESTRING (116.38805 39.90342, 116.38804 39.9... |
| 4 | 5 | 2009-02-25T09:47:03 | 2009-02-25T14:31:24 | 39259.779560 | 305.200501 | LINESTRING M (116.385256 39.90027 1235555223.0... | LINESTRING (116.38526 39.90027, 116.38525 39.9... |